import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { Observable } from 'rxjs';
import { JsonResult } from '../model/JsonResult';
import * as gc from 'src/app/global/const';
import { UUID } from 'angular2-uuid';
import { DatePipe } from '@angular/common';
import { TableStyleService, TableStyle } from './table-style.service';

@Injectable({
  providedIn: 'root'
})
export class LifeScheduleService {


  private path_pre = 'lifeSchedule';
  private tableName:string = 'life_schedule';
  private tableStyle:TableStyle[] = [];

  private url = {
    'deleteByPrimaryKey':gc.BACKEND_SERVER_ROOT + '/' + this.path_pre,
    'selectByPrimaryKey':gc.BACKEND_SERVER_ROOT + '/' + this.path_pre,
    'selectAll':gc.BACKEND_SERVER_ROOT + '/' + this.path_pre + '/a',
    'saveOne':gc.BACKEND_SERVER_ROOT + '/' + this.path_pre,
    'insert':gc.BACKEND_SERVER_ROOT + '/' + this.path_pre + '/insert',
    'update':gc.BACKEND_SERVER_ROOT + '/' + this.path_pre + '/update',
  }
  constructor(
    private http:HttpClient,
    private datePipe:DatePipe,
    private tableStyleService:TableStyleService
  ) { 
    this.tableStyleService.selectByTableName(this.tableName).subscribe(
      ( res:JsonResult ) => {
        if( res.code !==1 ){
          console.log(res.msg);
        }

        this.tableStyle = res.data;
      },error => console.log('获取表' + this.tableName + '的样式时出现异常！')
    )
  }

  deleteByPrimaryKey(iid:number):Observable<JsonResult>{
    const url = this.url.deleteByPrimaryKey + '/' + iid;
    return this.http.delete<JsonResult>(url);
  }

  selectByPrimaryKey(iid:number):Observable<JsonResult>{
    const url = this.url.selectByPrimaryKey + '/' + iid;
    return this.http.get<JsonResult>(url);
  }

  selectAll():Observable<JsonResult>{
    return this.http.get<JsonResult>(this.url.selectAll);
  }

  saveOne(record:LifeSchedule):Observable<JsonResult>{
    record = this.tease(record);
    console.log('即将要保存的行数据是：');
    console.log(record);
    if( record.iid === undefined || record.iid === null || record.iid<=0 )
      return this.insert(record);
    else
      return this.update(record);
  }
  
  insert(record:LifeSchedule):Observable<JsonResult>{
    record = this.tease(record);
    return this.http.post<JsonResult>(this.url.insert,record);
  }

  update(record:LifeSchedule):Observable<JsonResult>{
    record = this.tease(record);
    return this.http.put<JsonResult>(this.url.update,record);
  }

  // 将日期类型的字段转换为 Date 类型之后再转换为 DB 端设置的日期格式
  // 目的为防止后端做反序列化时由于日期格式不对导致转换失败报错
  tease(record:LifeSchedule):LifeSchedule{
    if( this.tableStyle === undefined || this.tableStyle === null || this.tableStyle.length<=0 ) return
    this.tableStyle.forEach(ele => {
      if( ele.tease.length !== undefined && ele.tease.length !== null && ele.tease.length>0 ){
        let fieldName:string = ele.field_name;
        if( record[fieldName] ) {
          let tmp:Date = new Date(record[fieldName]);
          record[fieldName] = this.datePipe.transform(tmp,ele.tease);
        }
      }
    });
    return record;
  }
  
}


// Author：chanchaw
// Generator Version：1.0 - 2019年11月16日 16点46分
// This Model Created：Mon Jan 13 2020 21:29:43 GMT+0800 (中国标准时间)
export class LifeSchedule{
  iid:number;
  type:string;
  content:string;
  create_time:string;
  dead_line:string;
  // dead_line_date:Date;
  progress:number;
  finished:number;
  finished_time:string;

  uuid:string;

  constructor(){
    this.uuid = UUID.UUID();
  }

}